package com.hznu.jspFinal.controller;

import com.hznu.jspFinal.entity.Major;
import com.hznu.jspFinal.myBeans.R;
import com.hznu.jspFinal.service.MajorService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Api(description = "专业管理器",value = "major")
@RestController
@RequestMapping("/major")
public class MajorController {
    @Autowired
    private MajorService majorService;

    @ApiOperation(value = "增加专业")
    @PostMapping("/addOne")
    public R addOne(@RequestBody Major major){
        try{
            if (majorService.selectByName(major.getName()) != null){
                return R.error("该专业已经存在");
            }
            majorService.insert(major);
            return R.ok();
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }

    @ApiOperation(value="删除该专业")
    @GetMapping("/delete/{id}")
    public R delete(@PathVariable Integer id){
        try{
            majorService.delete(id);
            return R.ok();
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }

    @ApiOperation(value="查找该专业")
    @GetMapping("/findByPrimaryKey/{id}")
    public R findByPrimaryKey(@PathVariable Integer id){
        try{
            return R.ok().put("data",majorService.selectByPrimaryKey(id));
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }

    @ApiOperation(value = "修改专业")
    @PostMapping("/update")
    public R updateMajor(@RequestBody Major major){
        try{
            majorService.update(major);
            return R.ok();
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }

    @ApiOperation(value = "查找某个系下的所有专业")
    @GetMapping("/findAllMajors/{departmentid}")
    public R findAllMajors(@PathVariable Integer departmentid){
        try{
            return R.ok().put("data",majorService.selectAllMajor(departmentid));
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }
}
